package com.easy.user.controller;

import com.easy.common.bean.User;

import com.easy.common.service.ILoginService;
import com.easy.common.service.IUserService;
import com.easy.common.util.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
//处理用户登录、验证码、密码重置功能
@RestController
@RequestMapping("/user/login")
@CrossOrigin
public class LoginController {
    @Autowired
    ILoginService loginService;
    @Autowired
    IUserService userService;

    //获取验证码
    @GetMapping("/captcha")//验证码
    public Result captcha(){
        return Result.success(CaptchaUtil.getCaptcha());
    }

    //用户登录
    @PostMapping("/userDoLogin")//登录
    public Result userLogin(User user,@RequestParam("captcha")String captcha,@RequestParam("key")String key){
        //验证码验证
        if(!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.fail(500,"验证码错误");
        }
        //登录
        try {
            String token = loginService.userLogin(user);
            //删除验证码
            return Result.success("登录成功",token);
        } catch (Exception e) {
            return Result.fail(500,e.getMessage());
        }
    }

    //管理员登录
    @PostMapping("/adminDoLogin")//登录
    public Result adminLogin(User user,@RequestParam("captcha")String captcha,@RequestParam("key")String key){
        //验证码验证
        if(!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.fail(500,"验证码错误");
        }
        //登录
        try {
            String token = loginService.adminLogin(user);
            //删除验证码
            return Result.success("登录成功",token);
        } catch (Exception e) {
            return Result.fail(500,e.getMessage());
        }
    }

    //获取用户信息
    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);//先拿令牌
        User user=loginService.logininfo(token);
        return Result.success(user);
    }

    //发送重置密码验证码
    @PostMapping("/send-reset-code")
    public Result sendResetCode(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String captcha = params.get("captcha");
        String key = params.get("key");

        // 验证码验证
        if (!CaptchaUtil.checkCaptcha(key, captcha)) {
            return Result.fail(500, "验证码错误");
        }

        // 验证邮箱是否存在
        User user = userService.getByEmail(email);
        if (user == null) {
            return Result.fail(500, "该邮箱未注册");
        }

        try {
            // 生成验证码
            String code = VerificationCodeUtil.generateCode();
            // 保存验证码
            VerificationCodeUtil.saveCode(email, code);
            // 发送邮件
            EmailUtil.sendEmail(email, code);

            return Result.success("验证码已发送至您的邮箱");
        } catch (Exception e) {
            return Result.fail(500, "验证码发送失败: " + e.getMessage());
        }
    }

    //验证重置密码验证码
    @PostMapping("/verify-reset-code")
    public Result verifyResetCode(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String code = params.get("code");
        // 验证验证码
        if (!VerificationCodeUtil.verifyCode(email, code)) {
            return Result.fail(500, "验证码错误或已过期");
        }

        return Result.success("验证成功");
    }

    //通过邮箱重置密码
    @PostMapping("/reset-password-by-email")
    public Result resetPasswordByEmail(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String password = params.get("password");

        try {
            // 更新用户密码
            boolean result = userService.resetPasswordByEmail(email, password);
            if (result) {
                return Result.success("密码重置成功");
            } else {
                return Result.fail(500, "密码重置失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "密码重置失败: " + e.getMessage());
        }
    }

}
